package com.cuckoo.cuckoo_app2.adapters;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.dao.CommissionManagement;

import java.util.List;

public class GenerationAdapter extends RecyclerView.Adapter<GenerationAdapter.GenerationViewHolder> {

    private List<CommissionManagement> dataList;
    private Context context;

    public GenerationAdapter(Context context, List<CommissionManagement> generationItems) {
        this.context = context;
        this.dataList = generationItems;
    }

    @NonNull
    @Override
    public GenerationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_generation_compon, parent, false);
        return new GenerationViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull GenerationViewHolder holder, int position) {
        CommissionManagement item = dataList.get(position);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        int screenWidth = displayMetrics.widthPixels;
        int itemSpacingPx = 50;
        int itemCount = dataList.size();
        int totalSpacing = (itemCount - 1);
        int itemWidthWithSpacing = (screenWidth - totalSpacing) / itemCount;
        int itemWidth = itemWidthWithSpacing - (itemCount > 1 ? itemSpacingPx : 0);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                itemWidth,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        holder.generation.setLayoutParams(params);
        holder.generation.setText(item.getTitle());
        String str = item.getValue().toString();
        if (item.getType().equals("cent")) {
            str += "%";
        }
        holder.percentage.setText(str);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    // 清空数据并通知更新
    public void clearData() {
        this.dataList.clear(); // 假设 mData 不是 null
        notifyDataSetChanged();
    }

    // 更新数据列表的方法
    public void updateData(List<CommissionManagement> dataList) {
        this.dataList = dataList; // 直接替换数据列表
        notifyDataSetChanged(); // 通知RecyclerView数据已更改
    }

    static class GenerationViewHolder extends RecyclerView.ViewHolder {
        TextView generation;
        TextView percentage;

        GenerationViewHolder(View itemView) {
            super(itemView);
            generation = itemView.findViewById(R.id.text_view_generation);
            percentage = itemView.findViewById(R.id.text_view_percentage);
        }
    }
}
